package com.fenxiangle.yueding.feature.login.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.fenxiangle.yueding.R;
import com.fenxiangle.yueding.entity.bo.LoginPram;
import com.fenxiangle.yueding.feature.common.view.MainActivity;
import com.fenxiangle.yueding.feature.mine.view.BindRecommendSuccessActivity;
import com.fenxiangle.yueding.framework.api.UserApi;
import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by Home on 2018/9/1.
 */

public class RecommendPeopleActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.edt_referees_no)
    EditText mEdtRefereesNo;
    @BindView(R.id.btn_bind_referees)
    TextView btnBindReferees;
    @BindView(R.id.no_bind_referees)
    TextView noBindReferees;
    private String mUserUid;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_recommend_people;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initData() {
        initToolBar(mToolbar, "推荐人", true, true);
        mUserUid = this.getIntent().getStringExtra("user_uid");

    }

    @OnClick({  R.id.no_bind_referees,R.id.btn_bind_referees})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.no_bind_referees:
                startActivity(new Intent(this, MainActivity.class));
                finish();
                break;
            case R.id.btn_bind_referees:
                if (TextUtils.isEmpty(mEdtRefereesNo.getText().toString())) {
                    showMsg("请输入推荐人ID");
                    return;
                }

                LoginPram loginPram = new LoginPram();
                loginPram.setRefereeUserUid(mEdtRefereesNo.getText().toString());
                loginPram.setUserUid(mUserUid);
                AM.api().createApiService(UserApi.class)
                        .bindingReferee(loginPram)
                        .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                        .subscribe(new Observer<String>() {
                            @Override
                            public void onSubscribe(Disposable d) {
                                showLoading();
                            }

                            @Override
                            public void onNext(String userInfoBo) {
                                dismissLoading();
                                showMsg("绑定成功！");
                                Intent intent=new Intent(RecommendPeopleActivity.this, BindRecommendSuccessActivity.class);
                               intent.putExtra("recommend_id",mEdtRefereesNo.getText().toString());
                                startActivity(intent);
                                finish();
                            }

                            @Override
                            public void onError(Throwable e) {
                                dismissLoading();
                                showMsg(e.getMessage());
                            }

                            @Override
                            public void onComplete() {
                                dismissLoading();
                            }
                        });
                break;
        }

    }

}
